package cn.changeforyou.cmd.generator.db.mybatisplus;

import cn.changeforyou.analyzer.javaparse.ProjectUtils;
import cn.changeforyou.cmd.generator.CmdGenerator;
import cn.changeforyou.cmd.generator.CmdGeneratorParameter;
import cn.changeforyou.cmd.generator.GeneratorContext;
import cn.changeforyou.cmd.generator.db.mybatisplus.parameters.ServiceGeneratorParameters;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beust.jcommander.Parameter;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.validation.constraints.NotEmpty;
import org.springframework.stereotype.Service;

/**
 * @author zhyu
 * @date 2021/1/30 14:42
 */
public class ServiceGenerator implements CmdGenerator {
	
	
	public static void main(String[] args) {
		ServiceGenerator gen = new ServiceGenerator();
		gen.start();
	}
	
	
	@Override
	public String getSign() {
		return "serviceGenerator";
	}

	@Override
	public String getName() {
		return "mybatis-plus service生成器";
	}

	@Override
	public Class getParameterClass() {
		return ServiceGeneratorParameters.class;
	}

	@Override
	public void init(GeneratorContext context) {
		
	}
	
	
	@Override
	public boolean generate_do(GeneratorContext context, CmdGeneratorParameter params) {
		ServiceGeneratorParameters serviceGeneratorParameters = (ServiceGeneratorParameters) params;
		@NotEmpty String projectPath = serviceGeneratorParameters.getProjectPath();
		@NotEmpty String entityName = serviceGeneratorParameters.getEntityName();
		@NotEmpty String servicePath = serviceGeneratorParameters.getServicePath();
		String entityPackage = ProjectUtils.findClassPackage(projectPath, entityName);
		String mapperPackage = ProjectUtils.findClassPackage(projectPath, entityName + "Mapper");
		String interface_service_package = ProjectUtils.getDirectoryPackageNameByMavenDefaultSetting(servicePath);
		
		boolean r1 = writeServiceFile(entityName, servicePath, entityPackage, interface_service_package);
		boolean r2 = writeServiceImplFile(entityName, servicePath, entityPackage, interface_service_package, mapperPackage);
		return r1 | r2;
	}
	
	private boolean writeServiceImplFile(String entityName, String servicePath, String entityPackage, String interface_service_package, String mapperPackage) {
		File serviceJavaFile = new File(servicePath + File.separator + "impl" + File.separator + entityName + "ServiceImpl.java");
		PrintWriter pw = null;
		try {
			if (serviceJavaFile.createNewFile()) {
				CompilationUnit compilationUnit = new CompilationUnit();
				compilationUnit.setPackageDeclaration(interface_service_package + ".impl");
				compilationUnit.addImport(ServiceImpl.class);
				compilationUnit.addImport(entityPackage + "." + entityName);
				compilationUnit.addImport(interface_service_package + "." + entityName + "Service");
				compilationUnit.addImport(mapperPackage + "." + entityName + "Mapper");
				ClassOrInterfaceDeclaration classOrInterfaceDeclaration = compilationUnit.addClass(entityName + "ServiceImpl");
				classOrInterfaceDeclaration.addExtendedType("ServiceImpl<" + entityName + "Mapper, " + entityName + ">");
				classOrInterfaceDeclaration.addImplementedType(entityName + "Service");
				classOrInterfaceDeclaration.addMarkerAnnotation(Service.class);
				Files.write(compilationUnit.toString().getBytes(), serviceJavaFile);
				System.out.println("ServiceImpl文件:" + entityName + "ServiceImpl已生成完毕");
				return true;
			} else {
				System.out.println("ServiceImpl文件:" + entityName + "ServiceImpl已存在");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != pw) {
				pw.close();
			}
		}
		return false;
	}
	
	private boolean writeServiceFile(String entityName, String servicePath, String entityPackage, String interface_service_package) {
		File serviceJavaFile = new File(servicePath + File.separator + entityName + "Service.java");
		PrintWriter pw = null;
		try {
			if (serviceJavaFile.createNewFile()) {
				CompilationUnit compilationUnit = new CompilationUnit();
				compilationUnit.setPackageDeclaration(interface_service_package);
				compilationUnit.addImport(IService.class);
				compilationUnit.addImport(entityPackage + "." + entityName);
				ClassOrInterfaceDeclaration classOrInterfaceDeclaration = compilationUnit.addInterface(entityName + "Service");
				classOrInterfaceDeclaration.addExtendedType("IService<" + entityName + ">");
				Files.write(compilationUnit.toString().getBytes(), serviceJavaFile);
				log.info("Service文件:" + entityName + "Service已生成完毕");
				return true;
			} else {
				log.info("Service文件:" + entityName + "Service已存在");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != pw) {
				pw.close();
			}
		}
		return false;
	}
	

	
	

	
}
